<?php

namespace IDock\Insurance\DBIC\Response;

use IDock\Http\Response;

class AllDBICResponse extends Response
{
    protected $records = [];

    protected $result = [];

    public function resolve(): Response
    {
        $body = $this->getBody();
        $body = json_decode($body, true);

        if ($this->printer) {
            $this->printer = new $this->printer;
        }

        $responseCode = $body['responseCode'] ?? '0';

        $errorMsg = $body['errorMsg'] ?? '';

        if ($responseCode != '1') {
            $this->setError($errorMsg);
            return $this;
        }
        $this->setBody($body ?? []);

        $this->result = $this->getBody()['result'] ?? [];
        $this->records = $this->result['data']['records'] ?? [];

        return $this;
    }

    public function getRecords(string $string = '', $default = '')
    {
        if (strlen($string)) {
            return $this->records[$string] ?? $default;
        }
        return $this->records;
    }

    public function getResult(string $string, $default = '')
    {
        if (strlen($string)) {
            return $this->result[$string] ?? $default;
        }
        return $this->result;
    }
}